package panel.login;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import frame.Login;
import function.CheckUsername;
import function.LogIn;
import function.SignUp;
import global.Const;
import other.DButton;
import panel.common.PasswordField;
import panel.common.TextField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;

public class SignUpPanel extends JPanel implements ActionListener,Runnable{
    private final DButton backButton = new DButton("返回");
    private final TextField userName = new TextField("用户名");
    private final PasswordField password = new PasswordField("密  码");
    private final DButton signUpButton = new DButton("注 册");
    private final DButton nextButton = new DButton("下一步");

    private Thread thread = new Thread(this);

    public SignUpPanel() {
        init();
    }

    public void init() {
        // 面板设置
        setBounds(0, 0, Const.PANEL_WIDTH, Const.PANEL_HEIGHT);
        setLayout(null);
        setOpaque(false);
        setVisible(false);

        // 文本框
        userName.setBounds(20, 400, 500, 64);
        userName.setMaxLength(16);
        add(userName);
        password.setBounds(20, 400, 500, 64);
        password.setVisible(false);
        password.setMaxLength(16);
        add(password);

        // 登录按钮
        DButton logInButton = new DButton("登录");
        logInButton.setFont(new Font("宋体", Font.BOLD, 24));
        logInButton.setBackground(new Color(42, 130, 228));
        logInButton.setBounds(440, 10, 90, 32);
        logInButton.setForeground(Color.white);
        logInButton.addActionListener(this);
        add(logInButton);

        // 返回按钮
        backButton.setFont(new Font("宋体", Font.BOLD, 24));
        backButton.setBounds(10, 10, 90, 32);
        backButton.addActionListener(this);
        backButton.setVisible(false);
        add(backButton);

        // 下一步按钮
        nextButton.setBounds(70, 800, 400, 64);
        nextButton.setOpaque(false);
        nextButton.addActionListener(this);
        add(nextButton);

        // 注册按钮
        signUpButton.setBounds(70, 800, 400, 64);
        signUpButton.addActionListener(this);
        signUpButton.setVisible(false);
        add(signUpButton);
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(10);
                repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.print(e.getActionCommand() + "\n");
        switch (e.getActionCommand()) {
            case "下一步":
                checkUsername();
                break;
            case "登录":
                close();
                Login.logInPanel.open();
                break;
            case "注 册":
                signUp();
                break;
            case "返回":
                userName.setVisible(true);
                password.setVisible(false);
                nextButton.setVisible(true);
                signUpButton.setVisible(false);
                backButton.setVisible(false);
                break;
        }
    }

    public void open() {
        setVisible(true);
    }

    public void close(){
        setVisible(false);
    }

    // 处理用户名检查
    private void checkUsername(){
        // 向服务器发送用户名检查请求
        SwingWorker<JSONObject, Void> worker = new SwingWorker<JSONObject, Void>() {
            @Override
            protected JSONObject doInBackground() {
                Login.loadingDialog.open();
                return CheckUsername.getReserve(userName.getText());
            }
            @Override
            protected void done() {
                try {
                    Login.loadingDialog.close();
                    JSONObject reserve = get();
                    if (reserve.getInteger("code") == 0) {
                        userName.setVisible(false);
                        password.setVisible(true);
                        nextButton.setVisible(false);
                        signUpButton.setVisible(true);
                        backButton.setVisible(true);
                    } else {
                        Login.messageDialog.open(reserve.getString("title"), reserve.getString("message"));
                    }
                } catch (ExecutionException | InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    Login.loadingDialog.close();
                }
            }
        };
        worker.execute();
    }

    // 处理注册请求
    private void signUp(){
        // 向服务器发送用户名检查请求
        SwingWorker<JSONObject, Void> worker = new SwingWorker<JSONObject, Void>() {
            @Override
            protected JSONObject doInBackground() {
                Login.loadingDialog.open();
                return SignUp.get(userName.getText(), password.getText());
            }
            @Override
            protected void done() {
                try {
                    Login.loadingDialog.close();
                    JSONObject reserve = get();
                    if (reserve.getInteger("code") == 0) {
                        setVisible(false);
                        Login.logInPanel.open();
                    } else {
                        Login.messageDialog.open(reserve.getString("title"), reserve.getString("message"));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    Login.loadingDialog.close();
                }
            }
        };
        worker.execute();
    }
}
